package com.yiren.dbaa.module.practice_set.carmodel.presenter;

import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHomeContract;
import com.yiren.dbaa.module.practice_set.carmodel.model.CarModelHomeModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/12/5 14:56
 * @description: 车模首页
 */
public class CarModelHomePresenter implements CarModelHomeContract.Presenter {

    private Activity activity;
    private CarModelHomeContract.Model carModelHomeModel;
    private UserParamConfigBean userParamConfigBean;

    public CarModelHomePresenter(Activity activity) {
        this.activity = activity;
        carModelHomeModel = new CarModelHomeModel(activity);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadCarModelList(CarModelHomeContract.View view) {
        //从服务器获取
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在加载..."));
        carModelHomeModel.getCarModelFromServer(userParamConfigBean.getUserInfo().getpUserId().toString(), false, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<CarModelEntity> serverModels = (List<CarModelEntity>) result;
                //获取本地列表
                List<CarModelEntity> localModels = UserDbController.getInstance(false).queryAll(CarModelEntity.class);
                if (null == serverModels) {
                    view.showCarModel(localModels);
                    return;
                }
                if (null == localModels) {
                    view.showCarModel(serverModels);
                    return;
                }
                //数据合并去重，服务器与本地数据都存在以本地数据为准
                view.showCarModel(carModelHomeModel.carModelHomeDataMerge(localModels, serverModels));
                super.success(result);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
                //获取失败加载本地
                List<CarModelEntity> localMaps = GradeDbController.getInstance(false).queryAll(CarModelEntity.class);
                for (CarModelEntity carModelEntity : localMaps) {
                    carModelEntity.setDownLoad(true);
                }
                view.showCarModel(localMaps);
            }
        });
    }

    @Override
    public void downLoadCarModel(CarModelEntity carModel, CarModelHomeContract.View view) {
        carModelHomeModel.downLoadCarModelPoint(carModel, new CommonCallBack() {
            @Override
            public void success(Object result) {
                List<CarModelEntity> carModelList = (List<CarModelEntity>) result;
                for (CarModelEntity carModelEntity : carModelList) {
                    if (UserDbController.getInstance(false).insertOrReplace(carModelEntity)) {
                        //通知视图
                        LoadDialog.dismiss(activity);
                        MessageBox.MessageAndVoiceModel(activity, "下载成功");
                        view.refreshCarModelList();
                    } else {
                        MessageBox.MessageAndVoiceModel(activity, "下载失败");
                    }
                }
            }

            @Override
            public void fail(String error) {
                MessageBox.MessageAndVoiceModel(activity, "下载失败");
            }
        });
    }

    @Override
    public void deleteCarModel(CarModelEntity carModel, CommonCallBack callBack) {
        //清除本地记录
        if (UserDbController.getInstance(false).delete(carModel)) {
            callBack.success("");
        }
    }
}
